//
//  ViewController.m
//  UI11_Model的使用
//
//  Created by LGC on 16/3/23.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "ViewController.h"
#import "TableViewOfContact.h"
#import "ModelOfContact.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) TableViewOfContact *tableView;

@property (nonatomic, retain) NSArray *arrOfKeys;

@property (nonatomic, retain) NSMutableDictionary *dicOfContact;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self createTableView];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - data
- (void)handleData {
    NSString *path = [[NSBundle mainBundle]pathForResource:@"DLS160101-2" ofType:@"plist"];
    
    NSDictionary *dicOfPlist = [NSDictionary dictionaryWithContentsOfFile:path];
    
    // 新字典初始化.
    self.dicOfContact = [NSMutableDictionary dictionary];
    // 构建新字典, 里面含有model
    for (NSString *key in dicOfPlist) {
      
        NSArray *arrOfContacts = [dicOfPlist objectForKey:key];
        // 创建新数组,用于存放model对象.
        NSMutableArray *marrOfModel = [NSMutableArray array];
        // 遍历数组
        for (NSDictionary *dic in arrOfContacts) {
            
            ModelOfContact *model = [[ModelOfContact alloc]init];
            // model 赋值(KVC)
            [model setValuesForKeysWithDictionary:dic];
            // model 放入新数组中
            [marrOfModel addObject:model];
            
            [model release];
            
        }
        // 将存放model的数组放入新字典中.
        [self.dicOfContact setObject:marrOfModel forKey:key];
    }
    self.arrOfKeys = [[self.dicOfContact allKeys] sortedArrayUsingSelector:@selector(compare:)];
 

}



- (void)createTableView {
    
    self.tableView = [[TableViewOfContact alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
    
    
    [self.view addSubview:self.tableView];
    
    [_tableView release];
    
}

#pragma mark - tableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
   
    return self.arrOfKeys.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSString *keys = [self.arrOfKeys objectAtIndex:section];
    
    NSArray *arr = [self.dicOfContact objectForKey:keys];
    
    return arr.count;
}
    
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    NSString *keys = [self.arrOfKeys objectAtIndex:indexPath.section];
    
    NSArray *arr = [self.dicOfContact objectForKey:keys];
    
    ModelOfContact *model = [arr objectAtIndex:indexPath.row];
    
    [model retain];
    
    cell.textLabel.text = model.name;
    cell.detailTextLabel.text = model.phoneNumber;
    
    [model release];
    return cell;
    
}

- (void)handleJSON {
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"news" ofType:@"txt"];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    // 核心API:
    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    NSLog(@"%@", result);
    
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [self.arrOfKeys objectAtIndex:section];
}

@end
